﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Bing.Maps;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Core;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Windows.Devices.Geolocation;
using Bing_Map.Model;

// La plantilla de elemento Control de usuario está documentada en http://go.microsoft.com/fwlink/?LinkId=234236

namespace Bing_Map.Map
{
    public sealed partial class BingMap : UserControl
    {
        private Geolocator geolocator;
        private CurrentPosition position;

        private List<EventLocation> events;

        public BingMap()
        {
            this.InitializeComponent();
            this.InitializeMap();
            this.PrintNearEvents();
        }

        private async void InitializeMap()
        {
            this.geolocator = new Geolocator();
            this.position = new CurrentPosition();
            this.events = new List<EventLocation>();
            Geoposition current = await this.geolocator.GetGeopositionAsync();
            map.Children.Add(position);
            geolocator.PositionChanged += new Windows.Foundation.TypedEventHandler<Geolocator, PositionChangedEventArgs>(PositionChanged);
        }

        private void PositionChanged(Geolocator sender, PositionChangedEventArgs args)
        {
            this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, new DispatchedHandler(
                () =>
                {
                    DisplayPosition(this, args);
                }));
        }

        private void DisplayPosition(object sender, PositionChangedEventArgs args)
        {
            Location location = new Location(args.Position.Coordinate.Latitude, args.Position.Coordinate.Longitude);
            MapLayer.SetPosition(this.position, location);
            map.SetView(location, 15.0f);
        }

        private IList<Event> GetEvents()
        {
            IList<Event> events = new List<Event>();

            Int32 id1 = 1;
            String name1 = "Fibers en concert";
            Double longitude1 = 42.50;
            Double latitude1 = 2.209;
            Event first = new Event(id1, name1, longitude1, latitude1);
            events.Add(first);

            return events;
        }

        public void PrintNearEvents()
        {
            IList<Event> eventsList = this.GetEvents();

            foreach (Event e in eventsList)
            {
                EventLocation eL = new EventLocation(e);
                Location eventLoc = new Location(e.Latitude, e.Longitude);
                // Save event
                this.events.Add(eL);
                // Print event
                MapLayer.SetPosition(eL, eventLoc);
            }
        }
    }
}
